﻿using System;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using Zero.POCO;
using Zero.POCO.Helper;

namespace DevExpress.MailClient.Win.Forms.Campagne
{
    public partial class frmEditMarque : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private bool _isMarqueModified;
        public Marque marque { get; set; }
        private bool _newMarque = true;
        public Guid MarqueId { get; set; }


        public frmEditMarque(Marque marquehist,bool newMarque)
        {
            InitializeComponent();
            DialogResult = DialogResult.Cancel;
            _newMarque = newMarque;
            if (_newMarque)
            {
                MarqueId = default(Guid);
                BBICancel.Enabled = false;
                BBINext.Enabled = false;
                BBIPrev.Enabled = false;
                BBIDelete.Enabled = false;
                SLUAnnonceur.Enabled = false;
                _isMarqueModified = true;
                

            }
            else
            {
                MarqueId = marquehist.id;
                SLUAnnonceur.SelectedText = marquehist.Type.ToString();
                _isMarqueModified = false;
            }
        }//ss

        private void ribbonControl_ItemClick(object sender, ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "BBISave":
                    {
                        _isMarqueModified = false;
                        marque = AnnonceurFactory.CreateMarque(TENom.Text, CBEDomaine.SelectedText.ToMarque());
                        //_editionAnnonceurPresenter.WriteChanges();

                        break;
                    }
                case "BBISaveClose":
                    {
                        _isMarqueModified = false;
                        DialogResult result = QueryClose();
                        marque = AnnonceurFactory.CreateMarque(TENom.Text, CBEDomaine.SelectedText.ToMarque());
                        //_editionAnnonceurPresenter.WriteChanges();
                        if (result == DialogResult.Yes) this.Close();
                        break;
                    }
                case "BBIDelete":
                    {
                        DialogResult result = QueryDelete();
                        if (result == DialogResult.Yes) this.Close();
                        break;
                    }
            }
        }



        DialogResult QueryDelete()
        {
            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.DeleteQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    DeleteRecord();
                    return DialogResult.Yes;
            }
        }

        private void DeleteRecord()
        {
            throw new NotImplementedException();
        }


        DialogResult QueryClose()
        {
            if (!_isMarqueModified)
                return DialogResult.Yes;

            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.SaveQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                    return DialogResult.Cancel;
                case DialogResult.No:
                    return DialogResult.No;
                default:
                    ApplyChanges();
                    return DialogResult.Yes;
            }
        }

        private void ApplyChanges()
        {
            throw new NotImplementedException();
        }

        private void TENom_TextChanged(object sender, EventArgs e)
        {
            _isMarqueModified = true;
        }

        private void CBEDomaine_TextChanged(object sender, EventArgs e)
        {
            _isMarqueModified = true;
        }

        private void frmEditMarque_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = QueryClose();
            e.Cancel = result == DialogResult.Cancel;
        }

        private void TENom_Properties_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (TENom.Text.Trim().Length <= 0)
                e.Cancel = true;
        }
    }
}